package com.ssm.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.security.auth.login.LoginContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.ssm.entity.Dept;
import com.ssm.entity.User;
import com.ssm.service.DeptService;
import com.ssm.service.UserService;

@Controller
@RequestMapping(value="/user")
public class UserController {
	//注入用户service
	@Autowired
	private UserService userService;
	
	//注入部门service
	@Autowired
	private DeptService deptService;
	
	/**
	 * 登录
	 */
	@RequestMapping(value="/login")
	public String login(String username,String password) {
		if("admin".equals(username) && "1234".equals(password)){
			return "redirect:/user/userList";
		}
		return "fail";
	}
	/**
	 * 自动跳转错误页面
	 */
	@RequestMapping(value="/loginPage")
	public String loginPage(){
		return "index";
	}
	/**
	 * 查询所有用户
	 */
	@RequestMapping(value="/userList")
	public String userList(ModelMap map){
		List<User> list = userService.listUser();
		System.out.println(list);
		map.addAttribute("list", list);
		return "userList";
	}
	/**
	 * 在执行其他方法之前会先执行此方法，获取部门信息
	 * @ModelAttribute
	 */
	@ModelAttribute("deptList")
	public List<Dept> deptList(){
		
		return deptService.deptList();
	}
	/**
	 * 跳转到添加页面
	 */
	@RequestMapping(value=("/addUserPage"),method=RequestMethod.GET)
	public String addUserPage(){
		
		return "addUserPage";
	}
	/**
	 * 添加用戶
	 * @param user 
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value=("/addUser"),method=RequestMethod.POST)
	public String addUser(MultipartFile file, User user) throws IllegalStateException, IOException{
		//图片上传
		String filePath = "E:\\upload";//保存图片的路径
    	//获取原始图片的拓展名
    	String originalFilename = file.getOriginalFilename();
    	//新的文件名字
    	String newFileName = UUID.randomUUID()+originalFilename;
    	System.out.println(newFileName);
    	//创建图片全路径对象
    	File targetFile = new File(filePath,newFileName);
    
    	file.transferTo(targetFile);
        user.setImage(newFileName);
        //保存用户
		userService.addUser(user);
		return "redirect:/user/userList";
	}
	/**
	 * 删除用户
	 * @param id 
	 */
	@RequestMapping(value=("/deleteUser"),method=RequestMethod.GET)
	public String deleteUser(Integer id) {
		userService.deleteUser(id);
		return "redirect:/user/userList";
	}
	
	/**
	 * 跳转到修改用户界面
	 * @param id 
	 */
	@RequestMapping(value=("/updatePage"),method=RequestMethod.GET)
	public String updatePage(Integer id,ModelMap map){
		User user = userService.getById(id);
		map.addAttribute("user", user);
		return "updatePage";
	}
	
	/**
	 *修改用户 
	 */
	@RequestMapping(value=("/updateUser"),method=RequestMethod.POST)
	public String updateUser(User user){
		userService.updateUser(user);
		return "redirect:/user/userList";
	}
}

